package logic

import (
	"cloud-disk/core/helper"
	"cloud-disk/core/internal/svc"
	"cloud-disk/core/internal/types"
	"cloud-disk/core/model"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShareBasicCreateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewShareBasicCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShareBasicCreateLogic {
	return &ShareBasicCreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ShareBasicCreateLogic) ShareBasicCreate(req *types.ShareBasicCreateRequest, userIdentity string) (resp *types.ShareBasicCreateResponse, err error) {
	resp = new(types.ShareBasicCreateResponse)
	ur, has, err := l.svcCtx.UserRepostioryModel.GetFileByIdentity(req.UserRepositoryIdentity)
	if err != nil {
		resp.Code = 100
		resp.Message = "分享失败 " + err.Error()
		return resp, nil
	}

	if !has {
		resp.Code = 100
		resp.Message = "分享失败，用户数据不存在"
		return resp, nil
	}

	sb := &model.ShareBasic{
		Identity:               helper.GetUUID(),
		UserIdentity:           userIdentity,
		RepositoryIdentity:     ur.ReoisitoryIdentity,
		ExpiredTime:            req.ExpiredTime,
		UserRepositoryIdentity: req.UserRepositoryIdentity,
	}
	_, err = l.svcCtx.ShareBasicModel.CreateShareBasic(sb)
	if err != nil {
		resp.Code = 100
		resp.Message = "分享失败 " + err.Error()
		return resp, nil
	}

	resp.Code = 200
	resp.Message = "分享成功"
	resp.Identity = sb.Identity
	return
}
